package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.*;
import com.example.springboot.entity.vo.DormUserRole;
import com.example.springboot.entity.vo.UserRole;
import com.example.springboot.mapper.DormManagerMapper;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.service.DormManagerService;
import com.example.springboot.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class DormManagerServiceImpl extends ServiceImpl<DormManagerMapper, DormManager> implements DormManagerService {

    /**
     * 注入DAO层对象
     */
    @Resource
    private DormManagerMapper dormManagerMapper;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMapper sysRoleMapper;
    /**
     * 宿管登录
     */
    @Override
    public DormManager dormManagerLogin(String username, String password) {
        QueryWrapper<DormManager> qw = new QueryWrapper<>();
        qw.eq("username", username);
        qw.eq("password", password);
        DormManager dormManager = dormManagerMapper.selectOne(qw);
        if (dormManager != null) {
            return dormManager;
        } else {
            return null;
        }
    }

    @Override
    public void addDorm(DormManager dormManager) {
        //新增用户
        //保存角色
        int insert=this.baseMapper.insert(dormManager);
        if (insert>0){
            SysUserRole role=new SysUserRole();
            role.setRoleId(dormManager.getRoleId());
            role.setUserId(dormManager.getUserId());
            sysUserRoleService.save(role);
        }

    }

    @Override
    public void updataDorm(DormManager dormManager) {

    }

    /**
     * 宿管新增
     */
    @Override
    public int addNewDormManager(DormManager dormManager) {
        int insert = dormManagerMapper.insert(dormManager);
        return insert;
    }

    /**
     * 宿管查找
     */
    @Override
    public Page find(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<DormManager> qw = new QueryWrapper<>();
        qw.like("name", search);
        Page dormManagerPage = dormManagerMapper.selectPage(page, qw);
        return dormManagerPage;
    }

    /**
     * 宿管信息更新
     */
    @Override
    public int updateNewDormManager(DormManager dormManager) {
        int i = dormManagerMapper.updateById(dormManager);
        return i;
    }

    /**
     * 宿管删除
     */
    @Override
    public int deleteDormManager(String username) {
        int i = dormManagerMapper.deleteById(username);
        return i;
    }

    /**
     * 分页查询 带角色
     */
    @Override
    public Page findList(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<DormManager> qw = new QueryWrapper<>();
        qw.like("user_id", search);
        Page dormManagerPage = dormManagerMapper.selectPage(page, qw);
        // 获取学生列表
        List<DormManager> dormManagers = dormManagerPage.getRecords();
        // 创建一个新的列表用于存储带有角色名称的学生信息
        List<DormUserRole> dormUserRoles = new ArrayList<>();
        for (DormManager dormManager : dormManagers) {
            // 查询每个学生的角色名称
            QueryWrapper<SysUserRole> query=new QueryWrapper<>();
            query.lambda().eq(SysUserRole::getUserId,dormManager.getUserId());
            SysUserRole one=sysUserRoleService.getOne(query);
            String roleName = "未知角色"; // 默认角色名称
            if (one != null) {
                // 查询角色名称
                SysRole name = sysRoleMapper.selectById(one.getRoleId());
                if (name != null) {
                    roleName = name.getRoleName(); // 更新角色名称
                }
            }
            // 创建新的对象并设置角色名称
            DormUserRole dormUserRole = new DormUserRole(dormManager, roleName);
            dormUserRoles.add(dormUserRole);

        }
        // 创建一个新的Page对象用于返回
        Page<DormUserRole> resultPage = new Page<>(pageNum, pageSize);
        resultPage.setRecords(dormUserRoles);
        resultPage.setTotal(dormManagerPage.getTotal());
        resultPage.setSize(dormManagerPage.getSize());
        resultPage.setCurrent(dormManagerPage.getCurrent());
        return resultPage;
    }

}
